package com.lyf.scm.admin.api.controller;

import com.lyf.scm.admin.api.dto.*;
import com.lyf.scm.admin.domain.service.RequirementPlanService;
import com.lyf.scm.common.enums.ResponseMsg;
import com.lyf.scm.common.model.ExcelExportDTO;
import com.lyf.scm.common.util.ExcelExportUtil;
import com.rome.arch.core.clientobject.Response;
import com.rome.arch.util.controller.RomeController;
import com.rome.user.common.utils.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 需求计划
 *
 * @author wangchlei
 * @date 2020/10/26
 */
@Slf4j
@Api(tags = "需求计划")
@RequestMapping("/api/v1/requirements")
@RomeController
public class RequirementPlanController {
    @Resource
    private RequirementPlanService requirementPlanService;

    @PostMapping("/listByWeek")
    @ApiOperation(value = "查询列表数据")
    public Response<PlanResponseDTO<RequirementPlanInfoDTO>> listWeekBySelective(@RequestBody RequirementPlanDTO requirementPlanDTO) {
        return Response.builderSuccess(requirementPlanService.listWeekBySelective(requirementPlanDTO));
    }

    @PostMapping("/confirmQty")
    @ApiOperation(value = "更新供应链确认量")
    public Response updateConfirmQty(@Validated @RequestBody List<RequirementConfirmationDTO> requirementConfirmationDTOs,@RequestParam("unitType") Long unitType) {
        requirementPlanService.updateConfirmQty(requirementConfirmationDTOs,unitType);
        return Response.builderSuccess(null);
    }
    @GetMapping("/planTypes")
    @ApiOperation(value = "查询所有计划类型")
    public Response<Map<String, String>> listAllPlanTypes() {
        return Response.builderSuccess(requirementPlanService.listAllPlanTypes());
    }
    @PostMapping("/eChartsData")
    @ApiOperation(value = "图表数据接口")
    public Response<EChartsDTO> eChartsData(@RequestBody RequirementPlanDTO requirementPlanDTO) {
        try {
            return Response.builderSuccess(requirementPlanService.eChartsData(requirementPlanDTO));
        } catch (feign.RetryableException e) {
            e.printStackTrace();
            return Response.builderFail(ResponseMsg.EXCEPTION_SC_1004.getCode(), ResponseMsg.EXCEPTION_SC_1004.getMsg());
        }
    }

    @PostMapping("/exportExcel")
    public Response<String> exportExcel(@Valid @RequestBody ExcelExportDTO excelExportDTO, @RequestParam Integer type, HttpServletResponse httpServletResponse){
        try {
            OperationLogRecordDTO recordDto = requirementPlanService.saveOperatorLog(excelExportDTO, type);
            requirementPlanService.createExportFile(recordDto, excelExportDTO, type, UserContext.getUserId());
        } catch (Exception e) {
            ExcelExportUtil.handleExceptionResponse(httpServletResponse, e);
        }
        return Response.builderSuccess("导出任务处理中，请至任务列表查看！");
    }
    @PostMapping("/import")
    @ApiOperation("导入")
    public Response<Boolean> importExcel(@RequestBody MultipartFile file,@RequestParam("unitType") Long unitType) {
        return Response.builderSuccess(requirementPlanService.importExcel(file,unitType));
    }
}
